package com.example.boke.service;


import com.example.boke.entity.User;
import com.example.boke.exception.ServiceException;
import com.example.boke.mapper.UserMapper;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.boke.utils.TokenUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;




/**
 * @author fx
 * @since 2025-02-22 16:17:39
 *@description
 */

@Service
public class UserService extends ServiceImpl<UserMapper,User>{

   @Resource
   UserMapper userMapper;


   @Override
   public boolean save(User entity) {
      if (StrUtil.isBlank(entity.getName())) {
         entity.setName(entity.getUsername());
      }
      if (StrUtil.isBlank(entity.getPassword())) {
         entity.setPassword("123");   // 默认密码123
      }
      if (StrUtil.isBlank(entity.getRole())) {
         entity.setRole("用户");   // 默认角色：用户
      }
      return super.save(entity);
   }

   public User selectByUsername(String username) {
      QueryWrapper<User> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("username", username);  //  eq => ==   where username = #{username}
      // 根据用户名查询数据库的用户信息
      return getOne(queryWrapper); //  select * from user where username = #{username}
   }

   // 验证用户账户是否合法
   public User login(User user) {
      User dbUser = selectByUsername(user.getUsername());
      if (dbUser == null) {
         // 抛出一个自定义的异常
         throw new ServiceException("用户名或密码错误");
      }
      if (!user.getPassword().equals(dbUser.getPassword())) {
         throw new ServiceException("用户名或密码错误");
      }
      // 生成token
      String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
      dbUser.setToken(token);
      return dbUser;
   }

   public User register(User user) {
      User dbUser = selectByUsername(user.getUsername());
      if (dbUser != null) {
         // 抛出一个自定义的异常
         throw new ServiceException("用户名已存在");
      }
      user.setName(user.getUsername());
      userMapper.insert(user);
      return user;
   }

   public void resetPassword(User user) {
      User dbUser = selectByUsername(user.getUsername());
      if (dbUser == null) {
         // 抛出一个自定义的异常
         throw new ServiceException("用户不存在");
      }
      if (!user.getPhone().equals(dbUser.getPhone())) {
         throw new ServiceException("验证错误");
      }
      dbUser.setPassword("123");   // 重置密码
      updateById(dbUser);
   }
}

